import QtQuick 2.0
import "Constants.js" as Constants
import "Utility.js" as Utils

Item {
    property int timer: 5
    property int power: 3
    property int index: 0
    signal explode

    /**
      This function returns array of indices for drawing explosion waves.
    */
    function getExplosionIndices(units, obstacles, bombs){
        var explosionIndices = new Array()
        explosionIndices.push(index)
        for (var i=1; i<=power; i++){
            var leftIndex = Utils.index(Utils.xFromIndex(index)-i, Utils.yFromIndex(index))
            if (leftIndex != -1){
                explosionIndices.push(leftIndex)
                if (obstacles[leftIndex] != null) {
                    break;
                }
            }
        }
        for (var i=1; i<=power; i++){
            var rightIndex = Utils.index(Utils.xFromIndex(index)+i, Utils.yFromIndex(index))
            if (rightIndex != -1){
                explosionIndices.push(rightIndex)
                if (obstacles[rightIndex] != null) {
                    break;
                }
            }
        }
        for (var i=1; i<=power; i++){
            var upIndex = Utils.index(Utils.xFromIndex(index), Utils.yFromIndex(index)-i)
            if (upIndex != -1){
                explosionIndices.push(upIndex)
                if (obstacles[upIndex] != null) {
                    break;
                }
            }
        }
        for (var i=1; i<=power; i++){
            var downIndex = Utils.index(Utils.xFromIndex(index), Utils.yFromIndex(index)+i)
            if (downIndex != -1){
                explosionIndices.push(downIndex)
                if (obstacles[downIndex] != null) {
                    break;
                }
            }
        }
        return explosionIndices;
    }

    FontLoader {
        id: bitfont
        source: "m04.ttf"
    }

    width: Constants.cellSize
    height: Constants.cellSize
    Image {
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        source: "gfx/bomb.png"
        z:50
        width: Constants.cellSize
        height: Constants.cellSize
    }
    Rectangle {
        color: "white"
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        width: 8
        height: 8
        Text {
            id: timerText
            text: timer
            font.family: bitfont.name
            font.pixelSize: 8
            z: 60
        }
    }
}
